package com.getpebble.android.main.sections.notifications;

import android.app.Activity;
import android.app.AlertDialog.Builder;
import android.app.LoaderManager;
import android.app.LoaderManager.LoaderCallbacks;
import android.content.DialogInterface;
import android.content.Loader;
import android.database.Cursor;
import android.os.Bundle;
import android.view.LayoutInflater;
import android.view.View;
import android.view.View.OnClickListener;
import android.view.ViewGroup;
import android.widget.Button;
import android.widget.CursorAdapter;
import android.widget.ListView;
import com.getpebble.android.PebbleApplication;
import com.getpebble.android.R;
import com.getpebble.android.common.core.trace.Trace;
import com.getpebble.android.common.framework.fragment.PblBaseFragment;
import com.getpebble.android.common.model.PblAndroidAppModel;
import com.getpebble.android.main.sections.notifications.adapter.AndroidAppAdapter;
import com.getpebble.android.notifications.util.NotificationUtil;

public class AllAppsFragment extends PblBaseFragment implements LoaderCallbacks<Cursor> {
    private static final String TAG = AllAppsFragment.class.getSimpleName();
    private CursorAdapter mCursorAdapter;
    private ListView mListView;

    public int getLayoutId() {
        return R.layout.fragment_all_apps;
    }

    public void init(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
        this.mListView = (ListView) container.findViewById(R.id.apps_list);
        Button buttonSelectNone = (Button) container.findViewById(R.id.select_none);
        ((Button) container.findViewById(R.id.select_all)).setOnClickListener(new OnClickListener() {
            public void onClick(View v) {
                PblAndroidAppModel.setAllAppsChosen(true, PebbleApplication.getAppContext().getContentResolver());
            }
        });
        buttonSelectNone.setOnClickListener(new OnClickListener() {
            public void onClick(View v) {
                PblAndroidAppModel.setAllAppsChosen(false, PebbleApplication.getAppContext().getContentResolver());
            }
        });
        Activity activity = getActivity();
        if (!(activity == null || NotificationUtil.isServiceEnabled(activity))) {
            new Builder(activity).setTitle(activity.getString(R.string.enable_notifications_dialog_title)).setMessage(R.string.enable_notifications_dialog_text).setPositiveButton(activity.getString(R.string.enable_notifications_service_dialog_btn_positive), new DialogInterface.OnClickListener() {
                public void onClick(DialogInterface arg0, int arg1) {
                    Activity innnerActivity = AllAppsFragment.this.getActivity();
                    if (innnerActivity != null) {
                        NotificationUtil.showEnableServiceActivity(innnerActivity);
                    }
                }
            }).setNegativeButton(activity.getString(R.string.enable_notifications_service_dialog_btn_negative), new DialogInterface.OnClickListener() {
                public void onClick(DialogInterface arg0, int arg1) {
                }
            }).setCancelable(false).show();
        }
        LoaderManager loaderManager = getLoaderManager();
        if (loaderManager != null) {
            loaderManager.initLoader(1234, null, this);
        }
    }

    public Loader<Cursor> onCreateLoader(int id, Bundle args) {
        switch (id) {
            case 1234:
                return PblAndroidAppModel.getCursorLoader(PebbleApplication.getAppContext());
            default:
                return null;
        }
    }

    public void onLoadFinished(Loader<Cursor> loader, Cursor data) {
        Activity activity = getActivity();
        if (activity == null) {
            Trace.debug(TAG, "Activity is null, ignoring onLoadFinished");
            return;
        }
        Trace.debug(TAG, "onLoadFinished size = " + data.getCount());
        if (this.mCursorAdapter == null) {
            this.mCursorAdapter = new AndroidAppAdapter(activity, data);
            this.mListView.setAdapter(this.mCursorAdapter);
            return;
        }
        this.mCursorAdapter.changeCursor(data);
    }

    public void onLoaderReset(Loader<Cursor> loader) {
    }
}
